import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import ThreadedAdbClient 1.0
import QtCore
import FlashTool 1.0
import QtQuick.Dialogs

Rectangle {
    color: "#f5f7fa"

    ColumnLayout {
        spacing: 20
        anchors.fill: parent
        anchors.margins: 15
        
        // Header
        Rectangle {
            Layout.fillWidth: true
            height: 60
            color: "#3498db"
            radius: 8
            
            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: 15
                anchors.rightMargin: 15
                
                Label {
                    text: "X3 Flash Tool"
                    font.bold: true
                    font.pixelSize: 18
                    color: "white"
                    Layout.alignment: Qt.AlignVCenter
                }
                
                Item { Layout.fillWidth: true }
                
                Rectangle {
                    width: 12
                    height: 12
                    radius: 6
                    color: flashTool.fastbootDeviceConnected ? "#2ecc71" : "#e74c3c"
                    Layout.alignment: Qt.AlignVCenter
                }
                
                Label {
                    text: flashTool.fastbootDeviceConnected ? "Device Connected" : "No Fastboot Device"
                    color: "white"
                    font.pixelSize: 12
                    Layout.alignment: Qt.AlignVCenter
                }
            }
        }

        // Device Status Cards
        GridLayout {
            Layout.fillWidth: true
            columns: 2
            columnSpacing: 15
            rowSpacing: 15
            
            // ADB Device Card
            Rectangle {
                Layout.fillWidth: true
                height: 120
                color: "white"
                radius: 8
                border.color: "#e1e5e9"
                border.width: 1
                
                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 12
                    
                    Label {
                        text: "ADB Device"
                        font.bold: true
                        font.pixelSize: 14
                        color: "#2c3e50"
                    }
                    
                    Label {
                        id: adbDeviceLabel
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        text: flashTool.adbDevices.length > 0 ? flashTool.adbDevices.join(", ") : "No device detected"
                        wrapMode: Text.Wrap
                        color: flashTool.adbDevices.length > 0 ? "#27ae60" : "#7f8c8d"
                        font.pixelSize: 12
                    }
                    
                    Button {
                        text: "Reboot to Fastboot"
                        enabled: flashTool.adbDeviceConnected
                        Layout.fillWidth: true
                        
                        background: Rectangle {
                            radius: 6
                            color: parent.enabled ? "#3498db" : "#bdc3c7"
                        }
                        contentItem: Text {
                            text: parent.text
                            color: "white"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 12
                            font.bold: true
                        }
                        
                        onClicked: {
                            flashTool.rebootToFastbootMode()
                        }
                    }
                }
            }
            
            // Fastboot Device Card
            Rectangle {
                Layout.fillWidth: true
                height: 120
                color: "white"
                radius: 8
                border.color: "#e1e5e9"
                border.width: 1
                
                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 12
                    
                    Label {
                        text: "Fastboot Device"
                        font.bold: true
                        font.pixelSize: 14
                        color: "#2c3e50"
                    }
                    
                    Label {
                        id: fastbootDeviceLabel
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        text: flashTool.fastbootDevices.length > 0 ? flashTool.fastbootDevices.join(", ") : "No device detected"
                        wrapMode: Text.Wrap
                        color: flashTool.fastbootDevices.length > 0 ? "#27ae60" : "#7f8c8d"
                        font.pixelSize: 12
                    }
                    
                    Button {
                        text: "Reboot Device"
                        enabled: flashTool.fastbootDeviceConnected
                        Layout.fillWidth: true
                        
                        background: Rectangle {
                            radius: 6
                            color: parent.enabled ? "#e67e22" : "#bdc3c7"
                        }
                        contentItem: Text {
                            text: parent.text
                            color: "white"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 12
                            font.bold: true
                        }
                        
                        onClicked: {
                            flashTool.rebootDevice()
                        }
                    }
                }
            }
        }

        // Firmware Flash Section
        Rectangle {
            Layout.fillWidth: true
            height: 100
            color: "white"
            radius: 8
            border.color: "#e1e5e9"
            border.width: 1
            
            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 12
                
                Label {
                    text: "Firmware Flash"
                    font.bold: true
                    font.pixelSize: 14
                    color: "#2c3e50"
                }
                
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 10
                    
                    TextField {
                        id: imageFileTextField
                        Layout.fillWidth: true
                        placeholderText: "Select firmware image file path..."
                        text: flashToolSettings.value("flashTool/firmwareImagePath", "").toString()
                        onTextChanged: flashToolSettings.setValue("flashTool/firmwareImagePath", text)
                        
                        background: Rectangle {
                            radius: 6
                            border.color: "#dce0e5"
                            border.width: 1
                            color: "#f8f9fa"
                        }
                    }
                    
                    Button {
                        text: "Browse..."
                        Layout.preferredWidth: 80
                        
                        background: Rectangle {
                            radius: 6
                            color: "#95a5a6"
                        }
                        contentItem: Text {
                            text: parent.text
                            color: "white"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 12
                            font.bold: true
                        }
                        
                        onClicked: {
                            fileDialog.open()
                        }
                    }
                    
                    Button {
                        text: "Flash Firmware"
                        enabled: flashTool.fastbootDeviceConnected && imageFileTextField.text.length > 0
                        Layout.preferredWidth: 120
                        
                        background: Rectangle {
                            radius: 6
                            color: parent.enabled ? "#e74c3c" : "#bdc3c7"
                        }
                        contentItem: Text {
                            text: parent.text
                            color: "white"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 12
                            font.bold: true
                        }
                        
                        onClicked: {
                            flashTool.flashFirmware(imageFileTextField.text)
                        }
                    }
                }
                
                Label {
                    text: !flashTool.fastbootDeviceConnected ? "⚠️ Connect a Fastboot device first" : 
                          imageFileTextField.text.length === 0 ? "⚠️ Select a firmware image file" : "✅ Ready to flash"
                    color: !flashTool.fastbootDeviceConnected || imageFileTextField.text.length === 0 ? "#e74c3c" : "#27ae60"
                    font.pixelSize: 11
                }
            }
        }

        // Log Section
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: "white"
            radius: 8
            border.color: "#e1e5e9"
            border.width: 1
            
            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 12
                
                RowLayout {
                    Layout.fillWidth: true
                    
                    Label {
                        text: "Operation Log"
                        font.bold: true
                        font.pixelSize: 14
                        color: "#2c3e50"
                    }
                    
                    Item { Layout.fillWidth: true }
                    
                    Button {
                        text: "Clear Log"
                        Layout.preferredWidth: 80
                        
                        background: Rectangle {
                            radius: 6
                            color: "#7f8c8d"
                        }
                        contentItem: Text {
                            text: parent.text
                            color: "white"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 11
                        }
                        
                        onClicked: {
                            textArea.clear()
                        }
                    }
                }
                
                ScrollView {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    
                    TextArea {
                        id: textArea
                        readOnly: true
                        wrapMode: TextArea.WrapAnywhere
                        font.family: "Consolas, Monaco, monospace"
                        font.pixelSize: 11
                        selectByMouse: true
                        
                        background: Rectangle {
                            color: "#1a1a1a"
                            radius: 4
                        }
                        
                        color: "#e0e0e0"
                        padding: 8
                    }
                }
            }
        }
    }

    FlashTool {
        id: flashTool
        onFlashOutput: function (output) {
            textArea.append(output)
            scrollToBottom()
        }
        onError: function (err) {
            textArea.append("❌ Error: " + err + "\n")
            scrollToBottom()
        }
    }

    FileDialog {
        id: fileDialog
        title: "Select Firmware Image File"
        currentFolder: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0]
        nameFilters: ["Image files (*.img *.zip *.tar)", "All files (*)"]
        onAccepted: imageFileTextField.text = selectedFile.toString().replace("file:///", "")
    }

    function scrollToBottom() {
        textArea.cursorPosition = textArea.text.length;
    }

    Settings {
        id: flashToolSettings
    }
}